/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package agent;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import mobileipsimulation.AgentInterface;

/**
 *
 * @author Rente
 */
public class BroadCastAdvertismentSender extends Thread {

    private DatagramSocket socket;
    private int port;
    private String pacote, ipAddress;
    private InetAddress group;
    static AgentInterface ai;
    private String flag;

    public BroadCastAdvertismentSender(int port, String ipAddress, String pacote, AgentInterface agi,String flag) {
        this.flag = flag;
        ai = agi;
        this.port = port;
        this.ipAddress = ipAddress;
        this.pacote = pacote;
        this.start();
    }

    @Override
    public void run() {
        while (true) {
            try {
                try {
                    this.group = InetAddress.getByName(ipAddress);
                    socket = new DatagramSocket();
                    byte[] mensage = pacote.getBytes();
                    DatagramPacket request = new DatagramPacket(mensage, mensage.length, group, port);
                    socket.send(request);
                   // ai.printMe(flag+": sending broadcast...");
                } catch (IOException ex) {
                    //Logger.getLogger(BroadCastAdvertismentSender.class.getName()).log(Level.SEVERE, null, ex);
                }
                Thread.sleep(2000);
            } catch (InterruptedException ex) {
                //Logger.getLogger(BroadCastAdvertismentSender.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
